﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, a, b values are filtered instead of R G B
    /// </summary>
    public class YCbCrFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_YInterval = new DoubleInterval(0.0f, 100.0f);
        protected DoubleInterval m_CbInterval = new DoubleInterval(0.0f, 10.0f);
        protected DoubleInterval m_CrInterval = new DoubleInterval(0.0f, 10.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public YCbCrFilter()
        {
        }

        /// <summary>
        /// Specify the interval
        /// </summary>
        /// <param name="_y">The _y.</param>
        /// <param name="_cb">The _CB.</param>
        /// <param name="_cr">The _CR.</param>
        public YCbCrFilter(DoubleInterval _y, DoubleInterval _cb, DoubleInterval _cr)
        {
            m_YInterval = _y;
            m_CbInterval = _cb;
            m_CrInterval = _cr;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_ycbcr">The _ycbcr.</param>
        internal YCbCrFilter(YCbCrFilter _ycbcr)
        {
            this.m_YInterval = _ycbcr.m_YInterval;
            this.m_CbInterval = _ycbcr.m_CbInterval;
            this.m_CrInterval = _ycbcr.m_CrInterval;
        }

        /// <summary>
        /// Gets or sets Y
        /// </summary>
        /// <value>The Y.</value>
        public DoubleInterval Y
        {
            get { return m_YInterval; }
            set { m_YInterval = value; }
        }

        /// <summary>
        /// Gets or sets cb
        /// </summary>
        /// <value>The cb.</value>
        public DoubleInterval Cb
        {
            get { return m_CbInterval; }
            set { m_CbInterval = value; }
        }

        /// <summary>
        /// Gets or sets Cr
        /// </summary>
        /// <value>The cr.</value>
        public DoubleInterval Cr
        {
            get { return m_CrInterval; }
            set { m_CrInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            YCbCr ycbcr = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    ycbcr = new YCbCr(bitmap.GetPixel(x, y));

                    if (m_YInterval.Contains(ycbcr.Y)
                        && m_CbInterval.Contains(ycbcr.Cb)
                        && m_CrInterval.Contains(ycbcr.Cr))
                    {
                        bitmap.SetPixel(x, y, m_ColorForeground);
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new YCbCrFilter(this);
        }
    }
}
